package com.lottery.im.module.result.adapter.result;

import android.annotation.SuppressLint;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MotionEvent;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lottery.im.R;
import com.lottery.im.api.result.entity.ResultInfo;
import com.lottery.im.constant.Constants;
import com.lottery.im.module.result.adapter.result.item.Horizontal115Adapter;
import com.lottery.im.module.result.adapter.result.item.HorizontalFastAdapter;
import com.lottery.im.module.result.adapter.result.item.HorizontalLotteryAdapter;
import com.lottery.im.module.result.adapter.result.item.HorizontalPkAdapter;
import com.lottery.im.util.TimerUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ResultAdapter extends BaseQuickAdapter<ResultInfo, ResultViewHolder> {

  public ResultAdapter(@Nullable List<ResultInfo> data) {
    super(R.layout.item_result_list, data);
  }

  @SuppressLint("ClickableViewAccessibility") @Override
  protected void convert(ResultViewHolder helper, final ResultInfo item) {
    helper.setText(R.id.tv_game_name, item.getGameName());
    String isSueId = item.getIssueId();
    helper.setText(R.id.tv_game_sueId,
        isSueId.length() >= 3 ? isSueId.substring(isSueId.length() - 3, isSueId.length()) + "期"
            : isSueId + "期");
    String gameType = item.getGameType();
    String resultStr = item.getResultStr();
    helper.setText(R.id.tv_game_lh, "");
    helper.setText(R.id.tv_he_des, "总和:");
    RecyclerView mListView = helper.getView(R.id.horizontal_list);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
    linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
    mListView.setLayoutManager(linearLayoutManager);
    mListView.setOnTouchListener((v, event) -> {
      if (event.getAction() == MotionEvent.ACTION_UP) {
        helper.itemView.performClick();//模拟父类控件的点击,解决点击事件冲突
      }
      return false;
    });
    showList(helper, gameType, resultStr, mListView);
    helper.time = helper.getView(R.id.tv_game_time);
    helper.closeTime = item.getGMTcloseTime();
    helper.curTime = item.getGMTcurTime();
    helper.isResult = item.getIsResult();
    if (helper.closeTime - helper.curTime > 0 && "1".equals(helper.isResult)) {
      if (helper.mCountDownTimer != null) {
        helper.mCountDownTimer.cancel();
      }
      helper.mCountDownTimer =
          new TimerUtils(helper.time, helper.closeTime - helper.curTime,
              Constants.COUNT_DOWN_INTERVAL);
      helper.mCountDownTimer.start();
    } else {
      if (helper.mCountDownTimer != null) {
        helper.mCountDownTimer.cancel();
      }
      helper.time.setText(R.string.opening);
    }
  }

  private void showList(ResultViewHolder helper, String gameType, String resultStr,
      RecyclerView mListView) {
    List<String> its = new ArrayList<>();
    if (Constants.lotteryArr.contains(gameType)) {
      if (resultStr.length() > 0) {
        String[] str = resultStr.split(",");
        if (str.length >= 5) {
          its.clear();
          its.addAll(Arrays.asList(str));
          mListView.setAdapter(new HorizontalLotteryAdapter(its));
          int w = Integer.parseInt(str[0]);
          int q = Integer.parseInt(str[1]);
          int b = Integer.parseInt(str[2]);
          int s = Integer.parseInt(str[3]);
          int g = Integer.parseInt(str[4]);
          int sum = w + q + b + s + g;
          String lh;
          String dx;
          String ds;
          if (w > g) {
            lh = "龙";
          } else if (w < g) {
            lh = "虎";
          } else {
            lh = "和";
          }
          if (sum >= 23) {
            dx = "大";
          } else {
            dx = "小";
          }
          if (sum % 2 == 0) {
            ds = "双";
          } else {
            ds = "单";
          }
          helper.setText(R.id.tv_game_lh, lh);
          helper.setText(R.id.tv_he_zhi, sum + "    " + dx + "    " + ds);
        }
      }
    }
    if (Constants.fastArr.contains(gameType)) {
      if (resultStr.length() > 0) {
        String[] str = resultStr.split(",");
        if (str.length >= 3) {
          its.clear();
          its.addAll(Arrays.asList(str));
          mListView.setAdapter(new HorizontalFastAdapter(its));
          int w = Integer.parseInt(str[0]);
          int q = Integer.parseInt(str[1]);
          int b = Integer.parseInt(str[2]);
          int sum = w + q + b;
          String dx;
          String ds;
          if (sum > 10) {
            dx = "大";
          } else {
            dx = "小";
          }
          if (sum % 2 == 0) {
            ds = "双";
          } else {
            ds = "单";
          }
          helper.setText(R.id.tv_he_zhi, sum + "    " + dx + "    " + ds);
        }
      }
    }
    if (Constants.pkArr.contains(gameType)) {
      helper.setText(R.id.tv_he_des, "冠亚和:");
      if (resultStr.length() > 0) {
        String[] str = resultStr.split(",");
        if (str.length >= 10) {
          its.clear();
          its.addAll(Arrays.asList(str));
          mListView.setAdapter(new HorizontalPkAdapter(its));
          int a1 = Integer.parseInt(str[0]);
          int a2 = Integer.parseInt(str[1]);
          int a3 = Integer.parseInt(str[2]);
          int a4 = Integer.parseInt(str[3]);
          int a5 = Integer.parseInt(str[4]);
          int a6 = Integer.parseInt(str[5]);
          int a7 = Integer.parseInt(str[6]);
          int a8 = Integer.parseInt(str[7]);
          int a9 = Integer.parseInt(str[8]);
          int a10 = Integer.parseInt(str[9]);
          int sum = a1 + a2;
          String lh1;
          String lh2;
          String lh3;
          String lh4;
          String lh5;
          String dx;
          String ds;
          if (a1 > a10) {
            lh1 = "龙";
          } else {
            lh1 = "虎";
          }
          if (a2 > a9) {
            lh2 = "龙";
          } else {
            lh2 = "虎";
          }
          if (a3 > a8) {
            lh3 = "龙";
          } else {
            lh3 = "虎";
          }
          if (a4 > a7) {
            lh4 = "龙";
          } else {
            lh4 = "虎";
          }
          if (a5 > a6) {
            lh5 = "龙";
          } else {
            lh5 = "虎";
          }
          if (sum > 11) {
            dx = "大";
          } else {
            dx = "小";
          }
          if (sum % 2 == 0) {
            ds = "双";
          } else {
            ds = "单";
          }
          helper.setText(R.id.tv_game_lh,
              lh1 + "    " + lh2 + "    " + lh3 + "    " + lh4 + "    " + lh5);
          helper.setText(R.id.tv_he_zhi, sum + "    " + dx + "    " + ds);
        }
      }
    }
    if (Constants.sd115Arr.contains(gameType)) {
      if (resultStr.length() > 0) {
        String[] str = resultStr.split(",");
        if (str.length >= 5) {
          its.clear();
          its.addAll(Arrays.asList(str));
          mListView.setAdapter(new Horizontal115Adapter(its));
          int w = Integer.parseInt(str[0]);
          int q = Integer.parseInt(str[1]);
          int b = Integer.parseInt(str[2]);
          int s = Integer.parseInt(str[3]);
          int g = Integer.parseInt(str[4]);
          int sum = w + q + b + s + g;
          String lh;
          String dx;
          String ds;
          if (w > g) {
            lh = "龙";
          } else if (w < g) {
            lh = "虎";
          } else {
            lh = "和";
          }
          if (sum == 30) {
            dx = "中";
          } else if (sum > 30) {
            dx = "大";
          } else {
            dx = "小";
          }
          if (sum % 2 == 0) {
            ds = "双";
          } else {
            ds = "单";
          }
          helper.setText(R.id.tv_game_lh, lh);
          helper.setText(R.id.tv_he_zhi, sum + "    " + dx + "    " + ds);
        }
      }
    }
    if (Constants.bj28Arr.contains(gameType)) {
      if (resultStr.length() > 0) {
        String[] str = resultStr.split(",");
        if (str.length >= 3) {
          its.clear();
          its.addAll(Arrays.asList(str));
          mListView.setAdapter(new HorizontalLotteryAdapter(its));
          int w = Integer.parseInt(str[0]);
          int q = Integer.parseInt(str[1]);
          int b = Integer.parseInt(str[2]);
          int sum = w + q + b;
          String dx;
          String ds;
          if (sum >= 14) {
            dx = "大";
          } else {
            dx = "小";
          }
          if (sum % 2 == 0) {
            ds = "双";
          } else {
            ds = "单";
          }
          helper.setText(R.id.tv_he_zhi, sum + "    " + dx + "    " + ds);
        }
      }
    }
  }
}